package com.grad.why.studentsystem.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.grad.why.studentsystem.domain.ChooseCourse;
import com.grad.why.studentsystem.domain.Course;
import com.grad.why.studentsystem.domain.Coursetype;
import com.grad.why.studentsystem.domain.Teacher;
import com.grad.why.studentsystem.domain.current.CurrentCourse;
import com.grad.why.studentsystem.domain.current.CurrentCourseDistributeInfos;
import com.grad.why.studentsystem.domain.current.CurrentProject;
import com.grad.why.studentsystem.mapper.*;
import com.grad.why.studentsystem.service.ClassesTableService;
import com.grad.why.studentsystem.service.CourseService;
import com.grad.why.studentsystem.util.Util;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;

@Component
@Service(interfaceClass = CourseService.class)
@Transactional
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private CoursetypeMapper coursetypeMapper;

    @Autowired
    private ChooseCourseMapper chooseCourseMapper;


    @Autowired
    private OptionparamsMapper optionparamsMapper;

    @Override
    public List<CurrentCourse> getCourseList(int offset, int limit, String search) {
        try{
            return courseMapper.getCourseList(offset,limit,search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getCourseListLength(String search) {
        try{
            return courseMapper.getCourseListLength(search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Coursetype> getCourseTypes() {
        try{
            return coursetypeMapper.getCourseTypes();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getMaxCourseId() {
        try{
            return courseMapper.getMaxCourseId();
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public boolean checkCourseExistByCourseName(String coursename) {
        try{
            if((courseMapper.getCourseInfoByCourseName(coursename))>0){
                return false;
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public int addCourseInfoByEntity(Course course) {
        try{
            return courseMapper.insertSelective(course);
        }catch (Exception e){

        }
        return 0;
    }

    @Override
    public int deleteCourseInfoById(int i) {
        try{
            return courseMapper.deleteByPrimaryKey(i);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateCourseInfoByEntity(Course course) {
        try{
            return courseMapper.updateByPrimaryKey(course);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Course> getCourseInfos() {
        try{
            return courseMapper.getCourseInfos();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getCourseStateByCid(int cid) {
        try{
            return courseMapper.getCourseStateByCid(cid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Course> getStudentCourseTableBySid(int sid) {
        try{
            return courseMapper.getCourseInfosBySid(sid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Course getCourseInfoByCid(int id) {
        try{
            return courseMapper.selectByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Course> getCourseInfosByRunning() {
        try{
            return courseMapper.getCourseInfosByRunning();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addCourseInfosByExcelFile(String uploadCurrentFileName) {
        try{
            InputStream is = new FileInputStream(uploadCurrentFileName);
            Workbook wb = null;

            if(uploadCurrentFileName.split("\\.")[1].equals("xls")){
                wb = new HSSFWorkbook(is);
            }else if(uploadCurrentFileName.split("\\.")[1].equals("xlsx")){
                wb = new XSSFWorkbook(is);
            }
            int sheetNumber = wb.getNumberOfSheets();
            int InsertCount = 0;

            Row title = wb.getSheetAt(0).getRow(0);
            //修改
            if(!title.getCell(0).getStringCellValue().equals("课程名称")||
                    !title.getCell(1).getStringCellValue().equals("学时")||
                    !title.getCell(2).getStringCellValue().equals("课程类型")||
                    !title.getCell(3).getStringCellValue().equals("教师编号")||
                    !title.getCell(4).getStringCellValue().equals("教师")||
                    !title.getCell(5).getStringCellValue().equals("起始周")||
                    !title.getCell(6).getStringCellValue().equals("终止周")||
                    !title.getCell(7).getStringCellValue().equals("绩点")){
                return 0;
            }
            for(int i = 0;i<sheetNumber;i++) {
                int index = 1;
                for(Row row : wb.getSheetAt(i)){
                    if(row == null){
                        continue;
                    }else{
                        if(index == 1){

                        }else{
                            if(row.getCell(0) == null){
                                index++;
                                continue;
                            }else{
                                Course course = new Course();
                                course.setCourseid(courseMapper.getMaxCourseId() + 1);
                                course.setCoursename(row.getCell(0).getStringCellValue());
                                course.setCoursetime((int)row.getCell(1).getNumericCellValue());
                                course.setCoursetype(row.getCell(2).getStringCellValue());
                                course.setCourseteacher((int)row.getCell(3).getNumericCellValue());
                                int startWeek = (int)row.getCell(5).getNumericCellValue();
                                int endWeek = (int)row.getCell(6).getNumericCellValue();
                                course.setCoursescore(Float.parseFloat(Util.formGetCellType(row.getCell(7))));
                                course.setCoursestartweek(startWeek);
                                course.setCourseendweek(endWeek
                                );
                                int thisWeek = Integer.parseInt(optionparamsMapper.getParamValueByParamName("thisweek"));
                                if(thisWeek < startWeek){
                                    course.setCoursestate("未开课");
                                }else if(thisWeek >= startWeek && thisWeek <= endWeek){
                                    course.setCoursestate("未结课");
                                }else if(thisWeek > endWeek){
                                    course.setCoursestate("结课");
                                }

                                courseMapper.insertSelective(course);
                                InsertCount = InsertCount + 1;
                            }
                        }
                        index++;
                    }
                }
            }
            return InsertCount;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<CurrentCourseDistributeInfos> AllCourseDistrubuteInfo() {
        try{
            return courseMapper.getAllCourseDistrubuteInfo();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
